Imports System.Xml.Serialization
Imports system.io
Imports System.Runtime.Serialization

Public Class AchievementDB
    Private AchievementXmlFilename As String = "AchievementDB.xml"
    Private ArmoryDataPath As String = "..\..\ArmoryData"

    Public Structure PlayerAchievement
        <XmlAttribute()> Dim ver As String
        <XmlAttribute()> Dim area As String
        <XmlAttribute()> Dim boss As String
        <XmlAttribute()> Dim note As String

        <XmlAttribute()> Dim d10 As String
        <XmlAttribute()> Dim d10H As String

        <XmlAttribute()> Dim d25 As String
        <XmlAttribute()> Dim d25H As String
    End Structure

    Public Achievements() As PlayerAchievement

    Private AchieveIdList() As String

    Public Sub New()
        ReDim Achievements(0)
        Achievements(0) = New PlayerAchievement
        Achievements(0).ver = "WoW"
        Achievements(0).area = "Deadmines"
        Achievements(0).boss = "Edwin VanCleef"
        Achievements(0).d10 = "3939"

        Dim filename As String
        filename = My.Computer.FileSystem.CombinePath(My.Computer.FileSystem.CombinePath(My.Computer.FileSystem.SpecialDirectories.AllUsersApplicationData, ArmoryDataPath), AchievementXmlFilename)

        Dim XU As New XmlUtils.AccessXml
        Try
            XU.ImportFromXml(Achievements, filename)
        Catch ex As Exception
        End Try

        InitAchieveIdList()
    End Sub

    Protected Overrides Sub Finalize()

        'Dim filename As String
        'filename = My.Computer.FileSystem.CombinePath(My.Computer.FileSystem.CombinePath(My.Computer.FileSystem.SpecialDirectories.AllUsersApplicationData, ArmoryDataPath), AchievementXmlFilename)

        'Dim XU As New XmlUtils.AccessXml
        'Try
        '    XU.ExportByXml(Achievements, filename)
        'Catch ex As Exception
        'End Try

        MyBase.Finalize()
    End Sub

    Private Sub InitAchieveIdList()
        Dim ct As Integer = 0
        Dim i As Integer

        For i = 0 To Achievements.Length - 1
            If Achievements(i).d10 <> "" Then
                ReDim Preserve AchieveIdList(ct)
                AchieveIdList(ct) = Achievements(i).d10
                ct += 1
            End If
            If Achievements(i).d10H <> "" Then
                ReDim Preserve AchieveIdList(ct)
                AchieveIdList(ct) = Achievements(i).d10H
                ct += 1
            End If
            If Achievements(i).d25 <> "" Then
                ReDim Preserve AchieveIdList(ct)
                AchieveIdList(ct) = Achievements(i).d25
                ct += 1
            End If
            If Achievements(i).d25H <> "" Then
                ReDim Preserve AchieveIdList(ct)
                AchieveIdList(ct) = Achievements(i).d25H
                ct += 1
            End If
        Next

        Array.Sort(AchieveIdList)
    End Sub

    Public Sub SetAchievementId(ByVal Xml As String, ByRef Achieve() As String)

        Dim XmlDoc As New Xml.XmlDocument
        Try
            XmlDoc.LoadXml(Xml)
        Catch ex As Exception
            Exit Sub
        End Try

        Dim node As Xml.XmlElement
        For Each node In XmlDoc.ChildNodes
            SetAchievementIdNode(node, Achieve, False)
        Next

    End Sub

    Private Sub SetAchievementIdNode(ByRef node As Xml.XmlElement, ByRef AchiData() As String, ByVal isFinished As Boolean)
        'done?
        Dim Done As Boolean = isFinished
        If Done = False Then
            Try
                Done = (node.GetAttribute("date") <> "")
            Catch ex As Exception
            End Try
        End If
        If Done = False Then
            Try
                Done = (node.GetAttribute("dateCompleted") <> "")
            Catch ex As Exception
            End Try
        End If

        Dim child As Xml.XmlElement
        For Each child In node.ChildNodes
            SetAchievementIdNode(child, AchiData, Done)
        Next

        If Done Then
            'id exists
            Dim id As String = ""
            Try
                id = node.GetAttribute("id")
            Catch ex As Exception
                id = ""
            End Try
            If id = "" Then Exit Sub

            'recordable id?
            Dim i As Integer = 0
            Dim acid As String = ""
            Dim acidAM As String = ""
            For i = 0 To AchieveIdList.Length - 1
                Dim isRecord As Boolean = False

                acid = AchieveIdList(i)
                If InStr(acid, "/") > 0 Then
                    acidAM = Mid(acid, InStr(acid, "/") + 1)
                    acid = Mid(acid, 1, InStr(acid, "/") - 1)
                Else
                    acidAM = ""
                End If

                If acidAM = id Then
                    isRecord = True
                ElseIf node.Name = "criteria" Then
                    If LCase(Left(acid, 1)) = "c" Then
                        If Mid(acid, 2) = id Then
                            isRecord = True
                        End If
                    End If
                Else
                    If LCase(Left(acid, 1)) <> "c" Then
                        If acid = id Then
                            isRecord = True
                        End If
                    End If
                End If

                If isRecord Then
                    'set achievement id
                    If AchiData Is Nothing Then
                        ReDim AchiData(0)
                        AchiData(0) = acid
                    Else
                        Dim j As Integer
                        Dim dataExist As Boolean = False
                        For j = 0 To AchiData.Length - 1
                            If AchiData(j) = acid Then
                                dataExist = True
                                Exit For
                            End If
                        Next

                        If Not dataExist Then
                            Dim ct = AchiData.Length
                            ReDim Preserve AchiData(ct)
                            AchiData(ct) = acid
                        End If
                    End If
                End If
            Next

        End If
    End Sub

    Public Function HasAchievement(ByVal id As String, ByRef AchiData() As String) As Boolean
        If AchiData Is Nothing Then
            Return False
        End If

        Dim i As Integer
        For i = 0 To AchiData.Length - 1
            If id = AchiData(i) Then
                Return True
            ElseIf InStr(AchiData(i), (id & "/")) = 1 Then
                Return True
            ElseIf InStr(id, (AchiData(i) & "/")) = 1 Then
                Return True
            End If
        Next

        Return False
    End Function
End Class
